package com.caipos.akka;

import akka.actor.AbstractExtensionId;
import akka.actor.ExtendedActorSystem;
import akka.actor.Extension;
import akka.actor.Props;
import org.springframework.context.ApplicationContext;

/**
 * Akka扩展，用于集成Spring的依赖注入
 */
public class AkkaSpringExtension extends AbstractExtensionId<AkkaSpringExtension.SpringExtension> {

    public static final AkkaSpringExtension SpringExtProvider = new AkkaSpringExtension();

    @Override
    public SpringExtension createExtension(ExtendedActorSystem system) {
        return new SpringExtension();
    }

    public static class SpringExtension implements Extension {
        private volatile ApplicationContext applicationContext;

        public void initialize(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }

        public Props props(String actorBeanName) {
            return Props.create(SpringActorProducer.class, applicationContext, actorBeanName);
        }
        
        public <T> T getInstance(Class<T> clazz) {
            return applicationContext.getBean(clazz);
        }
    }
}
    